﻿namespace CSharpPro._1016;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Programs
{
    class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }

    class Program
    {
        static List<User> users = new List<User>();

        static void Testprint()
        {
            while (true)
            {
                Console.WriteLine("\n主菜单");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择：");

                switch (Console.ReadLine())
                {
                    case "1": Login(); break;
                    case "2": Register(); break;
                    case "3": return;
                    default: Console.WriteLine("无效输入"); break;
                }
            }
        }

        static void Register()
        {
            Console.WriteLine("\n--- 注册 ---");
            
            string username;
            while (true)
            {
                Console.Write("用户名：");
                username = Console.ReadLine().Trim();

                if (string.IsNullOrWhiteSpace(username))
                    Console.WriteLine("用户名不能为空");
                else if (username.Length < 3 || username.Length > 15)
                    Console.WriteLine("长度需为3-15字符");
                else if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
                    Console.WriteLine("只能包含字母、数字、下划线");
                else if (users.Exists(u => u.Username == username))
                    Console.WriteLine("用户名已存在");
                else break;
            }
            
            string password;
            while (true)
            {
                Console.Write("密码：");
                password = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(password))
                    Console.WriteLine("密码不能为空");
                else if (password.Length < 6 || password.Length > 20)
                    Console.WriteLine("长度需为6-20字符");
                else if (!Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$"))
                    Console.WriteLine("需包含大小写字母和数字");
                else break;
            }
            
            Console.Write("确认密码：");
            if (Console.ReadLine() != password)
            {
                Console.WriteLine("两次密码不一致");
                return;
            }

            users.Add(new User { Username = username, Password = password });
            Console.WriteLine("注册成功！");
        }

        static void Login()
        {
            if (users.Count == 0)
            {
                Console.WriteLine("暂无用户，请先注册");
                return;
            }

            Console.WriteLine("\n--- 登录 ---");
            Console.Write("用户名：");
            string username = Console.ReadLine();

            Console.Write("密码：");
            string password = Console.ReadLine();

            User user = users.Find(u => u.Username == username);
            if (user == null)
            {
                Console.WriteLine("用户名不存在");
                return;
            }

            if (user.Password != password)
            {
                Console.WriteLine("密码错误");
                return;
            }

            Console.WriteLine($"\n登录成功！欢迎 {username}");
        }
    }
}
